<?php
declare(strict_types=1);

namespace App\Utils\Other\Gos\Goods;

use App\Utils\Other\Gos\App;
use Hyperf\Collection\Arr;
use Hyperf\Di\Annotation\Inject;
use Psr\SimpleCache\CacheInterface;
use Hyperf\Database\Model\ModelNotFoundException;

/**
 * Class Good
 * @author zx
 * @package App\Utils\Other\Gos\Goods
 */
Class Good extends App {

    /**
     * @var CacheInterface
     */
    #[Inject]
    protected CacheInterface $cache;

    /**
     * 商品详情
     * @param $id
     * @return array|mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function info($id, bool $isCache=true)
    {
        try {
            $this->setPath('publics/v1/goods');

            $keyName = $this->getPath().'_'.$id;
            if ($isCache === true && $this->cache->has($keyName)) {
                return Arr::get($this->cache->get($keyName), 'info');
            }

            $response = $this->request(['id'=>$id, 'auth'=>'xxxsssls']);
            if (!$response) {
                throw new ModelNotFoundException();
            }
            $this->cache->set($keyName, $response, 60*60*24*7); // 存储7天

            return Arr::get($response, 'info');
        } catch (\Throwable $throwable) {
            return [];
        }
    }
}